package models;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import models.PokerTP.Human;

public class Players {
	private static long nextId = 1;
	private static Map<Long, PlayPlayer> players = new HashMap<Long,PlayPlayer>();
	
	public static long addPlayer(String playerName, int tokensCount) {
		long lastId = nextId++;
		while(nextId != lastId) {
			if(nextId != 0 && !players.containsKey(nextId)) break;
			++nextId;
		}
		if(nextId == lastId) return 0; // nie znaleziono wolnego nr. id
		Human graczLudzki = new Human();
		graczLudzki.setNazwa(playerName);
		//PlayerInfo player = new PlayerInfo(nextId, graczLudzki);
		PlayPlayer player = new PlayPlayer();
		player.playerId = nextId;
		//player.setPlayerName(playerName);
		player.setNazwa(playerName);
		//player.setTokensCount(tokensCount);
		//player.setTokens(tokensCount);
		player.cash = tokensCount;
		players.put(nextId, player);
		return nextId++;
	}
	
	public static long addPlayer(String playerName) {
		return addPlayer(playerName, 0);
	}
	
	public static PlayPlayer getPlayer(long id) {
		return players.get(id);
	}
	
	public static boolean removePlayer(long id) {
		if(!players.containsKey(id)) return false;
		players.remove(id);
		return true;
	}
	
	public static boolean playerWithSameNameExists(String name) {
		PlayPlayer singlePlayer;
		Iterator<PlayPlayer> iterator = players.values().iterator();
		while(iterator.hasNext()) {
			singlePlayer = iterator.next();
			if(singlePlayer.getNazwa().compareTo(name) == 0)
				return true;
		}
		return false;
	}
}
